package util;

import java.awt.Rectangle;
import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;

import containers.Build;

import casino.Casino;

public class BuildGroup extends Rectangle implements Iterable<Build>{
	private CopyOnWriteArrayList<Build> builds = new CopyOnWriteArrayList<Build>();
	private static final int gap = 50;
	private float offset;
	private Casino casino;
	
	
	public Iterator<Build> iterator(){
		return new BuildIterator(builds);
	}
	
	public int size(){
		return builds.size();
	}
	
	public Build get(int i){
		return builds.get(i);
	}
	
	public BuildGroup(Casino c, int x, int y, int w){
		super(x,y,w,CardValues.height);
		casino = c;
		lengthChange();
	}
	
	public void addBuild(){
		if(builds.size() < 6){
			builds.add(new Build(casino));
			lengthChange();
		}
	}
	
	public boolean add(Build b){
		if(builds.size() < 6){
			builds.add(b);
			lengthChange();
			return true;
		}else{
			return false;
		}
	}
	
	public void remove(Build b){
		builds.remove(b);
		lengthChange();
	}
	
	public void lengthChange(){
		 for(int i = 0; i < builds.size(); i++){
			 offset = (float)(width - (CardValues.width*builds.size() + gap*builds.size()-1))/2;
			 if(offset < 0){
				 offset = 0;
				 float rgap = (float)(width-(builds.size()*CardValues.width))/(builds.size()-1);
				 builds.get(i).setPosition((int)(x+i*(CardValues.width+rgap)+offset), y);
			 }else{
				 builds.get(i).setPosition((int)(x+i*(CardValues.width+gap)+offset), y);
			 }
			 builds.get(i).lengthChange();
		 }
	}
}
